﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JXC.Application.Dtos;
using JXC.Application.Services.RoleInfoServices;
using JXC.Core.Models;
using JXC.Core.Repository;
using Microsoft.EntityFrameworkCore;

namespace JXC.Application.Services.RoleInfoServices
{
    public class RoleInfoService : IRoleInfoService
    {

        private readonly IRepository<RoleInfos, int> _repository;

        public RoleInfoService(IRepository<RoleInfos, int> repository)
        {
            _repository = repository;
        }
        public async Task<PagedResultDto<RoleInfos>> GetPagedResult(RoleInfoDto input)
        {
            var query = _repository.GetAll();
            if (!string.IsNullOrEmpty(input.RoleName))
            {
                query = query.Where(s => s.RoleName.Contains(input.RoleName));
            }
            //if (!string.IsNullOrEmpty(input.UserState))
            //{
            //    query = query.Where(s => s.UserName.Equals(input.UserState));
            //}

            var count = query.Count();
            //query = query.OrderBy(s => input.Sorting).Skip((input.Page - 1) * input.Limit).Take(input.Limit);
            var stus = await query.AsNoTracking().ToListAsync();
            var dtos = new PagedResultDto<RoleInfos>
            {
                TotalCount = count,
                Page = input.Page,
                Limit = input.Limit,
                Data = stus,
                FilterText = input.FilterText,
                Sorting = input.Sorting
            };
            return dtos;
        }
    }
}
